<template>
    <div class="wrapper">
        <el-row>
            <el-col :span="24">
                <common-card>
                    <template #header>
                        <div style="font-family: Roboto-Thin; padding: 20px; color: #fff">
                            <span style="font-size: 20px; display: inline-block; width: 100%">已选课题列表</span>
                            <span style="font-size: 14px; display: inline-block; width: 100%; margin-top: 10px">所有选题</span>
                        </div>
                    </template>
                    <template #main>
                        <el-table
                                v-loading="tableLoading"
                                :max-height="height"
                                :data="tableData"
                                stripe
                                style="width: 100%">
                            <el-table-column
                                    prop="taskname"
                                    label="课题名称">
                            </el-table-column>
                            <el-table-column
                                    prop="studentid"
                                    label="学生学号">
                            </el-table-column>
                            <el-table-column
                                    prop="studentname"
                                    label="学生姓名">
                            </el-table-column>
                            <el-table-column
                                    prop="replayscore"
                                    label="答辩成绩">
                            </el-table-column>
                            <el-table-column
                                    prop="finalscore"
                                    label="最终成绩">
                            </el-table-column>

                            <el-table-column
                                    prop="reportscore"
                                    label="报告成绩">
                            </el-table-column>
                            <el-table-column
                                    prop="teachername"
                                    label="小组导师">
                            </el-table-column>
                            <el-table-column
                                    prop="codefilepath"
                                    label="代码文件"
                                    width="150px">
                            </el-table-column>
                            <el-table-column
                                    prop="reportfilepath"
                                    label="报告文件"
                                    width="150px"
                                    >
                            </el-table-column>
                            <el-table-column
                                    label="操作"
                                    width="180">
                                <template slot-scope="scope">
                                    <upload-button text="上传代码文件" @upload="handleUpload"></upload-button>
                                    <upload-report-button text="上传报告文件" @upload="handleReportUpload"></upload-report-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </template>
                </common-card>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    import CommonCard from '../../components/Cards/CommonCard';
    import addDialog from './addDialog'
    import * as majorManage from '@/api/majorManage';
    import * as college from '@/api/college';
    import * as subjectManage from '@/api/subjectManage';
    import * as scoreManage from '@/api/scoreManage';
    import Cookie from "js-cookie";
    import uploadButton from "./uploadButton.vue";
    import {fileUpload} from '@/utils/upload.js'
    import uploadReportButton from "./uploadReportButton";

    export default {
        name: 'index',
        components: {
            CommonCard,
            addDialog,
            uploadButton,
            uploadReportButton
        },
        data() {
            return {
                total: 0,
                currentPage: 1,
                departmentData: {},
				subjectData: {},
                tableLoading: true,
                height: '',
                isAdd: false,
                isEdit: false,
                isStudent: false,
                tableData: [],
                options: [],
                options2: [],
                studentid: '',
                commitData: {
                    studentid: '',
                    teacherid: '',
                    subjectid: ''
                },
                uploadUrl: ''
            }
        },
        methods: {
            async handleUpload(formData) {
                await fileUpload('/task/upload/code', {formData,params:{studentid: this.studentid}});
                this.$notify({
                    title: '信息',
                    type: 'success',
                    message: '上传成功!'
                });
                this.fetchAllScore();
            },
            async handleReportUpload(formData){
                await fileUpload('/task/upload/report',{formData,params: {studentid: this.studentid}});
                this.$notify({
                    title: '信息',
                    type: 'success',
                    message: '上传成功!'
                });
                this.fetchAllScore();
            },
            async fetchAllScore() {
                try {
                    this.tableLoading = true;
                    const res = await scoreManage.fetchScoreTaskByCondition({studentid: this.studentid});
                    console.log(res.msg);
                    this.tableData = res.msg;
                } catch (e) {
                    console.log(e)
                } finally {
                    this.tableLoading = false;
                }
            },
            isOpenDialog(isOpen, isEdit) {
                this.isAdd = isOpen;
                this.isEdit = isEdit;
            },
            async getCollege () {
                const res = await college.fetchUserList();
                this.options = res.msg
            },
            async getMajor(){
                const res = await majorManage.fetchDepartmentList({});
                this.options2 = res.msg
            },
            async getStudentInfo(){
                let resInfo = JSON.parse(Cookie.get("userInfo"));
                console.log(resInfo);
                if (resInfo.role === 2){
                    this.studentid = resInfo.token;
                }
                console.log(this.studentid);
                if (this.studentid !== ''){
                    this.isStudent = true;
                }
            }
        },
        created () {
            this.height = window.innerHeight - 150;
            this.getStudentInfo();
            this.fetchAllScore();
            this.getCollege();
            this.getMajor();
            console.log(this.isStudent);
        }
        }
</script>

<style scoped>
    .wrapper {
        padding: 0 15px;
    }

    .tableSearch {
        width: 221px;
    }

    .tableSearch >>> .el-input__inner {
        border-radius: 415px;
    }

    .btn {
        margin-right: 7px;
    }
</style>